package org.display.util
{
	import flash.utils.*;
	
	import org.data.*;
	import org.net.*;
	import org.utils.*;
	public class CreateCommandControl
	{
		private var events:Array=[]
		
		public function CreateCommandControl()
		{
			var paths:Array=[]
			
			FileControl.readFile(paths,Global.workspace+"\\org\\commands")
			FileControl.readFile(paths,Global.workspace+"\\org\\business")
			
			events=paths
				
			for(var i=0;i<events.length;i++){
				
				events[i]=events[i].replace(/.*?(?=\/org\/)/,"").replace(".as","").replace(/\//gis,".").replace(/\./,"")
				
				//trace(events[i])
			}
				
			loadCommands()
			
		}
		
		function loadCommands(){
			
			var res:AbcResponder=new AbcResponder()
			
			res.onResult=function(e,path){
				
				var source:String=e
				
				source=repAbcController(source)
				source=repImport(source)
				source=repVar(source)
					
				if(Global.username!=""){
					
					source=source.replace("<username>",Global.username)
					
				}else{
				
					source=source.replace(".<username>","")
				}
				
				//trace(path,source)
				
				setTimeout(FileControl.saveFile,100,Global.workspace+"\\org\\control\\"+Global.username+"\\CommandControl.as",source)
				
			}
				
			var rootPath:String="AbcFrame/org/abcFrame/templete/"	
			
			new AbcHTTPServices(rootPath+"CommandControl.as",res)
			
		}
		
		function repAbcController(source:String):String{
		
		    for(var i=0;i<events.length;i++){
				
			var repStr="//AbcController"
			
			var newStr=repStr+"\n            AbcController.instance.registerCommand(\""+events[i]+"\")"
			
			source=source.replace(repStr,newStr)
		
			}
			
			return source
		
		}
		
		function repImport(source:String):String{
			
			for(var i=0;i<events.length;i++){
				
				var repStr="//import"
					
				var newStr=repStr+"\n         import "+events[i]+";"
				
				source=source.replace(repStr,newStr)
				
			}
			
			return source
			
		}
		
		function repVar(source:String):String{
			
			for(var i=0;i<events.length;i++){
				
				var arr:Array=events[i].split(".")
				
				var evtName:String=arr[arr.length-2]+"_"+arr[arr.length-1]+"_"+Math.floor(Math.random()*1000000)
				
				var repStr="//var"
				
				var newStr=repStr+"\n         private var "+evtName+":"+events[i]+";"
				
				source=source.replace(repStr,newStr)
				
			}
			
			return source
			
		}
		
		
		
	}
}